//
//  StatusPictureView.swift
//  SinaWeibo
//
//  Created by mac on 16/2/25.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit

class StatusPictureView: UICollectionView {
    //配置类型的属性
    private let pictureCellId = "pictureCellId"

        //配图视图的数据源数组
        var imageURLs: [NSURL]?{
            didSet{
               //计算配图视图的大小
                let pSize = calclatePictureViewSize()
                //更新配图视图的大小
                self.snp_updateConstraints { (make) -> Void in
//                    make.size.equalTo(pSize)
                    make.width.equalTo(pSize.width)
                    make.height.equalTo(pSize.height)
                }
    //刷新视图  tableView或collectionView的数据源更改就需要调用 reloadData
                reloadData()
                //更新测试label的文案
                testLabel.text = "\(imageURLs?.count ?? 0)"

            }
        }
    let itemWidth = (ScreenWidth - StatusCellMargin * 2 - 2 * PictureCellMargin) / 3
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        //实例化流水布局
        let flowLayout = UICollectionViewFlowLayout()
        //设置layout
        flowLayout.itemSize = CGSizeMake(itemWidth, itemWidth)
        //设置最小间距
        flowLayout.minimumInteritemSpacing = PictureCellMargin
        flowLayout.minimumLineSpacing = PictureCellMargin
        super.init(frame: CGRectZero, collectionViewLayout: flowLayout)
        self.registerClass(PictureCell.self, forCellWithReuseIdentifier: pictureCellId)
        //设置数据源
        self.dataSource = self
        //设置背景色
        self.backgroundColor = UIColor.whiteColor()

        
        //添加测试label
        self.addSubview(testLabel)
        testLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //1.根据配图视图的数量计算配图视图的真实大小
    private func calclatePictureViewSize() -> CGSize {
        //1.获取图片数量
        let imageCount = imageURLs?.count ?? 0
        if imageCount == 0{
            return CGSizeZero
        }
        //获取流水布局对象
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        //如果是单张图片
        if imageCount == 1{
            //返回和图片大小一样的视图
            let imageSize = CGSize(width: 150, height: 200)
//            //获取流水布局对象
//            let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
            //设置itemSize
            layout.itemSize = imageSize

            return imageSize
        }
        //如果不是单张图片 需要重新设置collectiView 的cell的大小  防止cell复用时候出现显示的问题
        layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        if imageCount == 4{
            //配图视图大小的计算
            let w = itemWidth * 2 + PictureCellMargin
            return CGSize(width: w, height: w)
        }
        let row = CGFloat((imageCount - 1) / 3 + 1)
        let w = itemWidth * 3 + PictureCellMargin * 2
        let h = itemWidth * row + (row - 1) * PictureCellMargin
        return CGSizeMake(w, h)
    }
    private lazy var testLabel: UILabel = UILabel(text: "", fontSize: 40, textColor: UIColor.redColor())

}
extension StatusPictureView: UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageURLs?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellId, forIndexPath: indexPath) as! PictureCell
        
        //设置调试颜色
        //indexPath.item % 2 == 0  判断奇数 偶数
        //cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.redColor() : UIColor.greenColor()
        cell.imageURL = imageURLs![indexPath.item]
        return cell
    }
}
//类之间关系很密切 并且没有太多复杂的逻辑 是可以写在同一个文件中
private class PictureCell: UICollectionViewCell {
    
    //外部设置的图片url属性
    var imageURL: NSURL? {
        didSet {
            iconView.sd_setImageWithURL(imageURL)
        }
    }
    
    
    //重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor(white: 0.95, alpha: 1)
        setupUI()
    }
    
    //默认实现报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置界面
    private func setupUI() {
        contentView.addSubview(iconView)
        //设置约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
    }
    
    //懒加载视图
    //private lazy var iconView: UIImageView = UIImageView()
    private lazy var iconView: UIImageView = {
        let iv = UIImageView()
        //UIImageView默认的显示方式  ScaleToFill: 缩放全部视图进行填充
        //显得很山寨
        //        iv.contentMode = .ScaleToFill
        // Aspect: 部分  缩放部分 进行填充  视图的比例不会失真 但是 图片会被裁剪
        iv.contentMode = .ScaleAspectFill
        //缩放图片  部分图片进行适应的显示  会导致 imageView可能会留白  一般也不推荐使用这样方式来显示图片
        //        iv.contentMode = .ScaleAspectFit
        //手动设置裁剪
        iv.clipsToBounds = true
        return iv
        }()

}

